package com.zeng.ssm.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 描述信息
 *
 * @author zengwanlin
 * @date 2023-08-29T10:32
 */
public class DateUtil {

    public static final String DATETIME14_PATTERN = "yyyyMMddHHmmss";

    public static final String DATE8_PATTERN = "yyyyMMdd";

    public static final String TIME6_PATTERN = "HHmmss";

    /**
     * 获取yyyyMMddHHmmss格式时间
     *
     * @return
     */
    public static String getDateTime() {
        return new SimpleDateFormat(DATETIME14_PATTERN).format(new Date());
    }

    /**
     * 将yyyyMMddHHmmss格式时间转换为Date
     *
     * @param dateTime
     * @return
     */
    public static Date dateTimeToDate(String dateTime) {
        try {
            return new SimpleDateFormat(DATETIME14_PATTERN).parse(dateTime);
        } catch (ParseException e) {
            throw new IllegalArgumentException("dateTime:" + dateTime, e);
        }
    }

    /**
     * 获取yyyyMMdd格式时间
     *
     * @return
     */
    public static String getDate8() {
        return new SimpleDateFormat(DATE8_PATTERN).format(new Date());
    }

    /**
     * 获取过期时间
     *
     * @param timeout 有效时间（S）
     * @return
     */
    public static String getExpireTime(long timeout) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.add(Calendar.SECOND, (int) timeout);
        return new SimpleDateFormat(DATETIME14_PATTERN).format(calendar.getTime());
    }

    /**
     * 获取HHmmss格式时间
     *
     * @return
     */
    public static String getTime6() {
        return new SimpleDateFormat(TIME6_PATTERN).format(new Date());
    }

}
